package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysNovelOrderMapper;
import com.ruoyi.system.domain.SysNovelOrder;
import com.ruoyi.system.service.ISysNovelOrderService;

/**
 * 订单Service业务层处理
 *
 * @author chj
 * @date 2022-11-09
 */
@Service
public class SysNovelOrderServiceImpl implements ISysNovelOrderService
{
    @Autowired
    private SysNovelOrderMapper sysNovelOrderMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public SysNovelOrder selectSysNovelOrderById(Long id)
    {
        return sysNovelOrderMapper.selectSysNovelOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param sysNovelOrder 订单
     * @return 订单
     */
    @Override
    public List<SysNovelOrder> selectSysNovelOrderList(SysNovelOrder sysNovelOrder)
    {
        return sysNovelOrderMapper.selectSysNovelOrderList(sysNovelOrder);
    }

    /**
     * 新增订单
     *
     * @param sysNovelOrder 订单
     * @return 结果
     */
    @Override
    public int insertSysNovelOrder(SysNovelOrder sysNovelOrder)
    {

        sysNovelOrder.setCreateTime(DateUtils.getNowDate());
        sysNovelOrder.setOrderAddtime(DateUtils.getNowDate());
        //定义随机订单编号
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++)
                fourRandom = "0" + fourRandom;
        }
        Date date = new Date();
        SimpleDateFormat dtf = new SimpleDateFormat("HHmmss");
        String last_time = dtf.format(date);
        //赋值给订单编号
        sysNovelOrder.setOrderNumber("DD"+fourRandom+last_time);

        //支付方式
        sysNovelOrder.setOrderPayType("支付宝");

        //如果订单已支付就添加支付时间
        if (sysNovelOrder.getOrderStatus().equals("1")){
            sysNovelOrder.setOrderPayTime(DateUtils.getNowDate());
        }

        //获取当前登入用户并赋值给实体类
        sysNovelOrder.setOrderUsername(SecurityUtils.getLoginUser().getUser().getUserName());
        return sysNovelOrderMapper.insertSysNovelOrder(sysNovelOrder);
    }

    /**
     * 修改订单
     *
     * @param sysNovelOrder 订单
     * @return 结果
     */
    @Override
    public int updateSysNovelOrder(SysNovelOrder sysNovelOrder)
    {
        sysNovelOrder.setUpdateTime(DateUtils.getNowDate());
        //如果订单已支付就添加支付时间
        if (sysNovelOrder.getOrderStatus().equals("1")){
            sysNovelOrder.setOrderPayTime(DateUtils.getNowDate());
        }
        return sysNovelOrderMapper.updateSysNovelOrder(sysNovelOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteSysNovelOrderByIds(Long[] ids)
    {
        return sysNovelOrderMapper.deleteSysNovelOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteSysNovelOrderById(Long id)
    {
        return sysNovelOrderMapper.deleteSysNovelOrderById(id);
    }
}
